﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Esint.Common.Data;
using Esint.JudicialAuction.Model;
using Esint.JudicialAuction.IDAL;
using Esint.Common.Model;
using System.Data.SqlClient;
using Esint.Common;

namespace Esint.JudicialAuction.SqlDAL
{
    /// <summary>
    /// 模块名称： 数据访问层
    /// 作    者：谷明
    /// 生成日期：2020年03月02日
    /// 生成模板: Esint.Template.SqlDAL.SqlDAL_11 版
    ///           适用于工厂模式架构
    /// 修改说明：
    /// </summary>
    public partial class Sys_UserData  : BaseData, ISys_UserData
    {
        /// <summary>
        /// 根据用户名和密码获取用户实体
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Sys_UserInfo GetUserModelByUserNameAndPwd(string userName, string password)
        {
            DataQuery query = new DataQuery();

            query.SQLText = "select * from T_Sys_User where userName=@userName and userPwd = @userPwd and isDelete=0";
            query.WhereParameters.Add(new WhereParameter("@userName", userName));
            query.WhereParameters.Add(new WhereParameter("@userPwd", password));

            return GetModelByQuery(query, CreateSys_UserInfoByReader);
        }

        /// <summary>
        /// 查询主列表（分页）
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="cellphone"></param>
        /// <param name="userType"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public ReturnTable GetMainList(string userName, string cellphone, string userType, string status, int pageSize, int pageIndex)
        {
            DataQuery query = new DataQuery();
            query.SQLText = @"select [userId]
      ,[userName]
      ,[userPwd]
      ,[cellphone]
      ,(select c.meaning from T_Sys_Code c where c.flag='1' and c.code=T_Sys_User.userType) [userTypeMeaning]
      ,(select c.meaning from T_Sys_Code c where c.flag='2' and c.code=T_Sys_User.status) [statusMeaning]
      ,ISNULL(CONVERT(VARCHAR(19),[lastLoginTime],120),'-') [lastLoginTime]
      ,[isDelete]
      ,[createUserId]
      ,[createTime]
      ,[opUserId]
      ,[opTime] from T_Sys_User where isDelete = 0";

            if (!string.IsNullOrWhiteSpace(userName))
            {
                query.SQLText += " and userName like @userName";
                query.WhereParameters.Add(new WhereParameter("@userName", userName));
            }

            if (!string.IsNullOrWhiteSpace(cellphone))
            {
                query.SQLText += " and cellphone like @cellphone";
                query.WhereParameters.Add(new WhereParameter("@cellphone", cellphone));
            }

            if (!string.IsNullOrWhiteSpace(userType))
            {
                query.SQLText += " and userType = @userType";
                query.WhereParameters.Add(new WhereParameter("@userType", userType));
            }

            if (!string.IsNullOrWhiteSpace(status))
            {
                query.SQLText += " and status = @status";
                query.WhereParameters.Add(new WhereParameter("@status", status));
            }

            ReturnTable rt = new ReturnTable();
            query.PageView = new PageSplit() { IsPageView = true, PageIndex = pageIndex, PageSize = pageSize };

            rt.Table = ExecuteDataTable(query);
            rt.PageInfo = query.PageView;
            return rt;
        }

        /// <summary>
        /// 批量删除用户方法
        /// </summary>
        /// <param name="idList"></param>
        public void DeleteUsers(List<string> idList)
        {
            DataQuery query = new DataQuery();

            var strWhere = GetListSqlAndParams(idList, query.WhereParameters);

            query.SQLText = $"update T_Sys_User set IsDelete=1  where UserId in {strWhere}";

            this.ExecuteNonQuery(new OperateObject(query));
        }

        /// <summary>
        /// 启用禁用用户方法
        /// </summary>
        /// <param name="idList"></param>
        /// <param name="IsDisabled"></param>
        public void DisabledUsers(List<string> idList, bool IsDisabled)
        {
            DataQuery query = new DataQuery();

            var strWhere = GetListSqlAndParams(idList, query.WhereParameters);

            query.SQLText = $"update T_Sys_User set [status]={(IsDisabled ? 1:0)}  where UserId in {strWhere}";

            this.ExecuteNonQuery(new OperateObject(query));
        }
    }
} 
